package org.example.gateway.spi.balancers;

import org.example.gateway.annoation.LoadBalance;
import org.example.gateway.contants.LoadBalanceConstants;
import org.example.gateway.pojo.dto.ServiceInstance;
import org.example.gateway.spi.LoadBalancer;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮询算法
 */
@LoadBalance(LoadBalanceConstants.ROUND)
public class FullRoundBalancer implements LoadBalancer {
    private volatile int index;

    @Override
    public ServiceInstance chooseOne(List<ServiceInstance> instances) {
        if (index == instances.size()){
            index = 0;
        }
        return instances.get(index++);
    }
}
